<?php
namespace app\plugs;

use app\util\MeiliSearch;

class Search extends Base{
    protected $info = [
        'name'  => '搜索', //插件名称
        'version'  => '1.0.0', //版本号
        'author'  => 'yh', //作者
        'desc'  => '搜索插件', //插件描述
        'status'  => '1', //插件状态 0禁用 1启用
        'command_id' => 1200, //指令id
        'git_url' => ''
    ];

    public function run() {
        $msg = $this->server->getMessage();
        $content = $msg['content']['text'];
        $ms = new MeiliSearch();
        list($indexName, $query) = explode(' ', $content);
        $hits = $ms->setIndex($indexName)->search($query);
        
        $text = '';
        foreach ($hits as $key => $hit) {
            $text .= <<<MEDIECATE
                {$hit['unique_record_id']}|{$hit['generic_name']}\t{$hit['producer_name']}\n
            MEDIECATE;
        }
        $this->server->responseText($text);
        return true;        //true 表示不再执行后续插件，false表示继续执行后续插件
    }
}